﻿using LdtJudge.DataEngine.Common.DataSave;
using LdtJudge.DataEngine.Data;
using LdtJudge.DataEngine.Data.DataParseStorage;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace LdtJudge.DataEngine.Common.CommHelp
{
    /// <summary>
    /// 分析数据保存类
    /// </summary>
    public class AnalysisDataControl
    {
        #region 基础数据
        /// <summary>
        /// 数据上下文
        /// </summary>
        public DataContext dataContext { get; set; }

        /// <summary>
        /// 数据库操作对象
        /// </summary>
        public SqlSysCommon sqlSysCommon = new SqlSysCommon("");


        #endregion

        /// <summary>
        /// 数据加载
        /// </summary>
        public AnalysisDataControl(DataContext dynamicStores)
        {
            dataContext = dynamicStores;
            //分析结束时间
            dataContext.DataLayerAns.FengXiCanShu.Jieshushijian = DateTime.Now;
        }

        #region 数据model填充
        /// <summary>
        /// 保存分析数据，保存的时候按照数据库的结构关系
        /// 自上而下的保存数据
        /// </summary>
        /// <param name="shujuyuanleixing">数据类型：1：卷宗；2：结构化；3：应用</param>
        public void SaveData(int shujuyuanleixing)
        {
            // 分析要素表保存数据
            //step 1 存储请求相关参数
            DAL_An_fengxicanshu canshudal = new DAL_An_fengxicanshu();
            canshudal.Insert(dataContext.DataLayerAns.FengXiCanShu);
            DAL_An_anjianxinxi anjianxin = new DAL_An_anjianxinxi();
            if (dataContext.DataLayerAns.Anjianxinxi != null)
            {
                var moxinxi = anjianxin.GetList(dataContext.DataLayerAns.Anjianxinxi.Anjianid);
                if (moxinxi.Count == 0)
                {

                    List<An_anyouxinxi> an_AnyouxinxiList = new List<An_anyouxinxi>();
                    if (dataContext.DataLayerAns.ExtractCaseCausesList != null && dataContext.DataLayerAns.ExtractCaseCausesList.Count > 0)
                    {
                        foreach (var casemodel in dataContext.DataLayerAns.ExtractCaseCausesList)
                        {
                            An_anyouxinxi an_Anyouxinxi = new An_anyouxinxi();
                            an_Anyouxinxi.Anyouxinxiid = Snowflake.Instance().GetId().ToString();
                            an_Anyouxinxi.Anjianid = dataContext.DataLayerAns.Anjianxinxi.Anjianid;
                            an_Anyouxinxi.Anyoucode = casemodel.CC_ID;
                            an_Anyouxinxi.Anyoumingcheng = casemodel.CC_Name;
                            an_Anyouxinxi.Kaishishijian = DateTime.Now;
                            an_AnyouxinxiList.Add(an_Anyouxinxi);

                        }
                    }
                    anjianxin.Insert(dataContext.DataLayerAns.Anjianxinxi);
                    if (an_AnyouxinxiList != null && an_AnyouxinxiList.Count > 0)
                    {
                        foreach (An_anyouxinxi anyouxinxi in an_AnyouxinxiList)
                        {
                            dataContext.InteractiveShow.LogShow("添加案由信息:" + JsonConvert.SerializeObject(anyouxinxi));
                            anjianxin.IntsertAnyouXini(anyouxinxi);
                        }
                    }
                }
                else
                {
                    if (dataContext.DataLayerAns.ExtractCaseCausesList != null && dataContext.DataLayerAns.ExtractCaseCausesList.Count > 0)
                    {
                        try
                        {

                            List<An_anyouxinxi> an_AnyouxinxiDelectList = anjianxin.GetanyouxinxiList(dataContext.DataLayerAns.Anjianxinxi.Anjianid);
                            if (an_AnyouxinxiDelectList != null && an_AnyouxinxiDelectList.Count > 0)
                            {
                                foreach (An_anyouxinxi anyouxinxi in an_AnyouxinxiDelectList)
                                {
                                    dataContext.InteractiveShow.LogShow("删除案由信息!!!"+JsonConvert.SerializeObject(anyouxinxi));
                                     anjianxin.DeletetAnyouXini(anyouxinxi);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            dataContext.InteractiveShow.LogShow("删除案由信息失败!" + ex.ToString());
                        }
                        List<An_anyouxinxi> an_AnyouxinxiList = new List<An_anyouxinxi>();
                        foreach (var casemodel in dataContext.DataLayerAns.ExtractCaseCausesList)
                        {
                            An_anyouxinxi an_Anyouxinxi = new An_anyouxinxi();
                            an_Anyouxinxi.Anyouxinxiid = Snowflake.Instance().GetId().ToString();
                            an_Anyouxinxi.Anjianid = dataContext.DataLayerAns.Anjianxinxi.Anjianid;
                            an_Anyouxinxi.Anyoucode = casemodel.CC_ID;
                            an_Anyouxinxi.Anyoumingcheng = casemodel.CC_Name;
                            an_Anyouxinxi.Kaishishijian = DateTime.Now;
                            an_AnyouxinxiList.Add(an_Anyouxinxi);

                        }
                        if (an_AnyouxinxiList != null && an_AnyouxinxiList.Count > 0)
                        {
                            foreach (An_anyouxinxi anyouxinxi in an_AnyouxinxiList)
                            {
                                dataContext.InteractiveShow.LogShow("添加案由信息:" + JsonConvert.SerializeObject(anyouxinxi));
                                anjianxin.IntsertAnyouXini(anyouxinxi);
                            }
                        }
                    }
                    
                    anjianxin.Update(dataContext.DataLayerAns.Anjianxinxi);
                }
            }

            ////step 3 存储分析卷宗相关信息
            //An_shujuneirong shujuneirongmo = new An_shujuneirong()
            //{
            //    Shujuid = DataSnowflake.Instance().GetId().ToString(),
            //    Anjianid = anjianxinmo.Anjianid,
            //    Fl_id = dataContext.DataLayerAns.FL_ID,
            //    Sf_id = dataContext.DataLayerAns.SF_ID,//analysisParameterModel.sourceDossierInfo.SF_ID,
            //    //Jieduan =,
            //    Shujuyuanbianhao = fengxicanshu.Juanzongbianhao,
            //    Shujuyuandierbiaoshi = analysisParameterModel.sourceDossierInfo.SD_SourceSecondSign,
            //    Shujuyuanleixing = shujuyuanleixing,
            //    Shujuyuanmingcheng = GetDocCHName(fengxicanshu.Anjianleixing, fengxicanshu.Juanzongbianhao),
            //    Shujuyuanzhuangtai =2,
            //};
            DAL_An_shujuneirong shujuneirongdal = new DAL_An_shujuneirong();
            DAL_An_anjianshiti anjianshitidal = new DAL_An_anjianshiti();
            //查询到与当前标识数据一样的内容后进行更新为删除；

            var shujulist = shujuneirongdal.GetListWhere(dataContext.DataLayerAns.Shujuneirong);
            if (shujulist.Count > 0)
            {
                for (int i = 0; i < shujulist.Count; i++)
                {
                    var oldshuju = shujulist[i];
                    oldshuju.Shujuyuanzhuangtai = 0;
                    shujuneirongdal.Update(oldshuju);
                    //查询到当前卷宗关联的实体进行标记删除
                    
                    var listshiti = anjianshitidal.GetListWhere(dataContext.DataLayerAns.Shujuneirong.Anjianid, oldshuju.Shujuid, 0);
                    for (int j = 0; j < listshiti.Count; j++)
                    {
                        var motemp = listshiti[j];
                        motemp.Zhuangtai = 0;
                        anjianshitidal.Update(motemp);
                    }

                }
            }
            dataContext.DataLayerAns.Shujuneirong.Shujuyuanzhuangtai = 1;
            //插入新的内容处理数据
            shujuneirongdal.Insert(dataContext.DataLayerAns.Shujuneirong);

            //保存结构化数据
            DataResultSave dataResultSave = new DataResultSave(dataContext);
            dataResultSave.AnalysisDataSaveNew(dataContext.DataLayerAns.Shujuneirong);
            //step 4 保存卷宗的段落信息
            try
            {
                DAL_An_duanluo duanluodal = new DAL_An_duanluo();
                if (dataContext.DataLayerAns.Duanluos == null)
                    dataContext.DataLayerAns.Duanluos = new List<An_duanluo>();
                foreach (var duanluo in dataContext.DataLayerAns.Duanluos)
                {
                    duanluo.Shujuid = dataContext.DataLayerAns.Shujuneirong.Shujuid;
                    var res = float.TryParse(duanluo.DuanluoxuhaoDesc, out float xuhao);
                    if (!res) xuhao = 0;
                    duanluo.Duanluoxuhao = xuhao;
                    duanluodal.Insert(duanluo);
                }
                DAL_An_anjianshitijuese anjianshitijuesedal = new DAL_An_anjianshitijuese();
                DAL_An_fayanrenduanluoxinxi fayanrenduanluoxinxidal = new DAL_An_fayanrenduanluoxinxi();
                if (dataContext.DataLayerAns.Anjianshitis != null)
                {
                    foreach (var anjianshiti in dataContext.DataLayerAns.Anjianshitis)
                    {//step 5 保存案件的实体信息
                        anjianshiti.Anjianid = dataContext.DataLayerAns.Anjianxinxi.Anjianid;
                        anjianshitidal.Insert(anjianshiti);
                    }
                }

                if (dataContext.DataLayerAns.Anjianshitijueses != null)
                {
                    foreach (var anjianshitijuese in dataContext.DataLayerAns.Anjianshitijueses)
                    {//step 6 保存案件的角色信息
                        anjianshitijuesedal.Insert(anjianshitijuese);
                    }
                }

                if (dataContext.DataLayerAns.Fayanrenduanluoxinxis != null)
                {
                    foreach (var fayanrenduanluoxinxi in dataContext.DataLayerAns.Fayanrenduanluoxinxis)
                    {//step 7 保存卷宗发言人信息
                        fayanrenduanluoxinxi.Shujuid = dataContext.DataLayerAns.Shujuneirong.Shujuid;
                        fayanrenduanluoxinxidal.Insert(fayanrenduanluoxinxi);
                    }
                }
            }
            catch (Exception)
            {


            }
        }
        #endregion

        #region 数据库操作
        /// <summary>
        /// 获取文书中文名称
        /// </summary>
        /// <param name="caseType">案件类型</param>
        /// <param name="docId">文书ID</param>
        /// <returns></returns>
        public string GetDocCHName(string caseType, string docId)
        {
            string docCHName = "";
            if (string.IsNullOrEmpty(caseType) || string.IsNullOrEmpty(docId))
            {
                docCHName = "";
            }
            else
            {
                var sysDocTypeTable = sqlSysCommon.GetDocTypeDataByCaseTypeID(caseType);
                var wheremo = sysDocTypeTable.Where(a => a.CD_Code == docId).FirstOrDefault();
                if (wheremo != null)
                {
                    docCHName = wheremo.CD_Name.ToString();
                }
            }

            return docCHName;
        }
        #endregion

    }
}
